package example.com.zcq.mymj.activity;

import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.inputmethod.InputMethodManager;
import android.widget.SearchView;
import android.widget.TextView;

import java.util.HashMap;

import example.com.zcq.mymj.R;
import example.com.zcq.mymj.bean.ChengYu;
import example.com.zcq.mymj.net.HttpInterface;
import example.com.zcq.mymj.net.RetrofitHttp;
import example.com.zcq.mymj.ui.TopBar;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class ChengYuActivity extends AppCompatActivity {

    private TopBar mTopbarTitle;
    private SearchView mSvCus;
    /**
     * 马到成功
     */
    private TextView mTvCy;
    /**
     * 拼音：hao
     */
    private TextView mTvPy;
    /**
     * 意思：哈市发货付款啦左右
     */
    private TextView mTvCyjx;
    /**
     * hasfhahsdkfahaksfalfa
     * laksjdflajsl
     * asjdlkajf;afjaldkla
     * fasfdla
     * fhakshdfas
     * fhashfda
     */
    private TextView mTvXiangjie;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_cheng_yu);
        initView();
    }

    private void initView() {
        mTopbarTitle = (TopBar) findViewById(R.id.topbar_title);
        mSvCus = (SearchView) findViewById(R.id.sv_cus);
        mTvCy = (TextView) findViewById(R.id.tv_cy);
        mTvPy = (TextView) findViewById(R.id.tv_py);
        mTvCyjx = (TextView) findViewById(R.id.tv_cyjx);
        mTvXiangjie = (TextView) findViewById(R.id.tv_xiangjie);

        mSvCus.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String s) {
                if (mSvCus != null) {
                    // 得到输入管理对象
                    InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                    if (imm != null) {
                        // 这将让键盘在所有的情况下都被隐藏，但是一般我们在点击搜索按钮后，输入法都会乖乖的自动隐藏的。
                        imm.hideSoftInputFromWindow(mSvCus.getWindowToken(), 0); // 输入法如果是显示状态，那么就隐藏输入法

                    }
                    mSvCus.clearFocus(); // 不获取焦点
                    search(s);

                }


                return true;
            }

            // 当搜索内容改变时触发该方法
            @Override
            public boolean onQueryTextChange(String s) {

                return false;
            }
        });
    }

    private void search(final String s) {
        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("key", "ae3238061d7ac629b497102a6168ebb2");
        hashMap.put("word", s);
        HttpInterface h = RetrofitHttp.getSimpleRetrofit();
        h.chengyu(hashMap).enqueue(new Callback<ChengYu>() {
            @Override
            public void onResponse(Call<ChengYu> call, Response<ChengYu> response) {
                ChengYu body = response.body();
                if (body != null) {
                    ChengYu.ResultBean re = body.getResult();
                    if (re != null) {
                        mTvCy.setText(s);
                        mTvCyjx.setText("词语解释：" + re.getCiyujs());
                        mTvPy.setText("拼音：" + re.getPinyin());
                        mTvXiangjie.setText("详解：" + re.getYinzhengjs());
                    }

                }

            }

            @Override
            public void onFailure(Call<ChengYu> call, Throwable t) {

            }
        });

    }
}
